package leetcode.struct;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    @Override
    public String toString() {
        String s = "";
        s += val + ">";
        if (left == null) {
            s += "null>";
        } else {
            s += left.toString();
        }
        if (right == null) {
            s += "null>";
        } else {
            s += right.toString();
        }
        return s;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, new TreeNode(6),
                new TreeNode(7)));
        root.preOrderTraversalRecursion(root);
        root.inOrderTraversalRecursion(root);
        root.postOrderTraversalRecursion(root);
        root.preOrderTraversal(root);
        root.inOrderTraversal(root);
        root.postOrderTraversal2Stack(root);
        root.postOrderTraversal1Stack(root);
        root.levelOrderTraversal(root);
        root.morrisPre(root);
        root.morrisIn(root);
        root.morrisPost(root);
    }

    public void preOrderTraversalRecursion(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.println(root.val);
        preOrderTraversalRecursion(root.left);
        preOrderTraversalRecursion(root.right);
    }

    public void inOrderTraversalRecursion(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrderTraversalRecursion(root.left);
        System.out.println(root.val);
        inOrderTraversalRecursion(root.right);
    }

    public void postOrderTraversalRecursion(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrderTraversalRecursion(root.left);
        postOrderTraversalRecursion(root.right);
        System.out.println(root.val);
    }

    public void preOrderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                System.out.println(root.val);
                root = root.left;
            }
            root = stack.pop();
            root = root.right;
        }
    }

    public void inOrderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            System.out.println(root.val);
            root = root.right;
        }
    }

    public void postOrderTraversal2Stack(TreeNode root) {
        Stack<TreeNode> stack1 = new Stack<>(), stack2 = new Stack<>();
        while (root != null || !stack1.isEmpty()) {
            while (root != null) {
                stack1.push(root);
                stack2.push(root);
                root = root.right;
            }
            root = stack1.pop();
            root = root.left;
        }
        while (!stack2.isEmpty()) {
            System.out.println(stack2.pop().val);
        }
    }

    public void postOrderTraversal1Stack(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if (root.right == null || pre == root.right) {
                System.out.println(root.val);
                pre = root;
                root = null;
            } else {
                stack.push(root);
                root = root.right;
            }
        }
    }

    public void morrisPre(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode mostRight = null;
        while(root != null) {
            mostRight = root.left;
            if(mostRight != null) {
                while(mostRight.right != null && mostRight.right != root) {
                    mostRight = mostRight.right;
                }
                if(mostRight.right == null) {
                    mostRight.right = root;
                    System.out.println(root.val);
                    root = root.left;
                    continue;
                } else {
                    mostRight.right = null;
                }
            } else {
                System.out.println(root.val);
            }
            root = root.right;
        }
    }

    public void morrisIn(TreeNode root) {
        if(root == null){
            return;
        }
        TreeNode mostRight = null;
        while(root != null) {
            mostRight = root.left;
            if(mostRight != null) {
                while(mostRight.right != null && mostRight.right != root) {
                    mostRight = mostRight.right;
                }
                if(mostRight.right == null) {
                    mostRight.right = root;
                    root = root.left;
                    continue;
                }else{
                    mostRight.right = null;
                }
            } else {
            }
            System.out.println(root.val);
            root = root.right;
        }

    }

    public void morrisPost(TreeNode root) {
        if(root == null) {
            return;
        }
        TreeNode mostRight = null, cur = root;
        while(cur != null) {
            mostRight = cur.left;
            if(mostRight != null) {
                while(mostRight.right != null && mostRight.right != cur){
                    mostRight = mostRight.right;
                }
                if(mostRight.right == null){
                    mostRight.right = cur;
                    cur = cur.left;
                    continue;
                } else {
                    mostRight.right = null;
                    morrisPostPrint(cur.left);
                }
            }
            cur = cur.right;
        }
        morrisPostPrint(root);
    }

    private void morrisPostPrint(TreeNode node) {
        TreeNode tail = reverseList(node);
        while(tail!=null){
            System.out.println(tail.val);
            tail = tail.right;
        }
        reverseList(tail);
    }
    private TreeNode reverseList(TreeNode node) {
        TreeNode pre = null, next = null;
        while(node != null) {
            next = node.right;
            node.right = pre;
            pre = node;
            node = next;
        }
        return pre;
    }

    public void levelOrderTraversal(TreeNode root) {
        if (root == null) {
            return;
        }
        List<TreeNode> list = new LinkedList<>();
        list.add(root);
        System.out.println(root.val);
        while (!list.isEmpty()) {
            root = list.get(0);
            list.remove(0);
            if (root.left != null) {
                list.add(root.left);
                System.out.println(root.left.val);
            }
            if (root.right != null) {
                list.add(root.right);
                System.out.println(root.right.val);
            }
        }

    }
}